function DRV_Timerfcns(hObj,evnt,tfcn) %#ok<INUSL>
global G_OBJ 

persistent RV 

switch tfcn
    
    case 'Start'
        sch = GetSchedule;
        CreateFigure([sch.boxid]);
        RV = zeros(1,length(sch)+1);
        
        
    case 'Timer'
        sch = GetSchedule;
        DRVfig = LocateFigure;
        if isempty(DRVfig)
            DRVfig = CreateFigure([sch.boxid]);
        end
        tableh = findobj(DRVfig,'Tag','DRVtable');
        
        for i = 1:length(sch)
            midx = findincell(strcmp('response_code',sch(i).readparams));
            midx = sch(i).readmodule(midx);
            rcstr = sprintf('response_code~%d',sch(i).boxid);
            RV(sch(i).trial_idx,i+1) = G_OBJ(midx).GetTagVal(rcstr);
        end
        
        RV(end,1) = sch(1).response_vals(end,1);
        
        set(tableh,'Data',RV);
        
        
    case 'Stop'
        clear RV
        
        
    case 'Error'
        
        
    otherwise
        warning(sprintf('DRV_Timerfcns:Unknown subfunction ''%s''',tfcn)) %#ok<SPWRN>
end



function f = CreateFigure(boxids)
f = findobj('Tag','DRVfig');
if isempty(f)
    f = figure( ...
        'Tag',      'DRVfig',           ...
        'Name',     'Lick Counts',      ...
        'Position', [100 250 612 533],  ...
        'Resize',   'on',               ...
        'Toolbar',  'none');
end

cnames = cell(1,length(boxids)+1);
cnames{1} = 'TrialID';
for i = 1:length(boxids)
    cnames{i+1} = sprintf('Box %d',boxids(i));
end

uitable( ...
    'Parent',           f,                                      ...
    'Data',             zeros(1,length(boxids)),                ...
    'Units',            'normalized',                           ...
    'Position',         [0.025 0.025 0.95 0.95],                ...
    'ColumnName',       cnames,                                 ...
    'ColumnFormat',     repmat({'numeric'},1,length(cnames)),   ...
    'RowStriping',      'on',                                   ...
    'BackgroundColor',  [1 1 1; .8 1 1],                        ...
    'Tag',              'DRVtable');

figure(f);
drawnow expose


function f = LocateFigure
f = findobj('Tag','DRVfig');


function sch = GetSchedule
f = findobj('Tag','SetupFig');
h = guidata(f);
sch = h.schedule;

